ifelse(!require(readr), install.packages('readr'), library(readr))
## Loading required package: readr
## Warning: package 'readr' was built under R version 3.5.3
## [1] "readr"
etf4_csv<-read_csv("ETF4_2000_2018_d.csv")
## Parsed with column specification:
## cols(
##   `<c3><U+04A8><e9><U+00A5>N<U+00BD>X` = col_character(),
##   `2<U+00BA><d9>` = col_character(),
##   `<U+00A4><e9><U+00B4><c1>` = col_double(),
##   `<U+00A5><U+00BC><U+00BD><U+057E><U+39AC><U+00BD>L<U+00BB><f9>(<U+00A4><U+00B8>)` = col_double(),
##   `<U+00B7><ed><U+00A4><U+99E1><U+00BB><f9>(<U+00A4><U+00B8>)` = col_double()
## )
etf4_csv<-read_csv("ETF4_2000_2018_d.csv", locale = locale(encoding='big5'))
## Parsed with column specification:
## cols(
##   證券代碼 = col_character(),
##   簡稱 = col_character(),
##   日期 = col_double(),
##   `未調整收盤價(元)` = col_double(),
##   `當日均價(元)` = col_double()
## )
head(etf4_csv)
## # A tibble: 6 x 5
##   證券代碼 簡稱           日期 `未調整收盤價(元)` `當日均價(元)`
##   <chr>    <chr>         <dbl>              <dbl>          <dbl>
## 1 0050     元大台灣50 20090105               34.2           34.3
## 2 0056     元大高股息 20090105               13.9           14  
## 3 0050     元大台灣50 20090106               34.2           34.2
## 4 0056     元大高股息 20090106               14.0           14.0
## 5 0050     元大台灣50 20090107               34.6           34.6
## 6 0056     元大高股息 20090107               14.3           14.3
str(etf4_csv)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 7493 obs. of  5 variables:
##  $ 證券代碼        : chr  "0050" "0056" "0050" "0056" ...
##  $ 簡稱            : chr  "元大台灣50" "元大高股息" "元大台灣50" "元大高股息" ...
##  $ 日期            : num  20090105 20090105 20090106 20090106 20090107 ...
##  $ 未調整收盤價(元): num  34.2 13.9 34.2 14 34.6 ...
##  $ 當日均價(元)    : num  34.3 14 34.2 14 34.6 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   證券代碼 = col_character(),
##   ..   簡稱 = col_character(),
##   ..   日期 = col_double(),
##   ..   `未調整收盤價(元)` = col_double(),
##   ..   `當日均價(元)` = col_double()
##   .. )
etf4.c<-etf4_csv[, c(-2, -4)]
etf4.c<-etf4.c[-1,]
colnames(etf4.c)<-c("id", "date", "price")
library(magrittr)
## Warning: package 'magrittr' was built under R version 3.5.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
etf4.c<-etf4_csv%>%select(c(1,3,5))%>%rename("id" = "證券代碼", "date"= "日期", "price" = "當日均價(元)")
etf4.c
## # A tibble: 7,493 x 3
##    id        date price
##    <chr>    <dbl> <dbl>
##  1 0050  20090105  34.3
##  2 0056  20090105  14  
##  3 0050  20090106  34.2
##  4 0056  20090106  14.0
##  5 0050  20090107  34.6
##  6 0056  20090107  14.3
##  7 0050  20090108  33.2
##  8 0056  20090108  13.9
##  9 0050  20090109  32.3
## 10 0056  20090109  13.6
## # ... with 7,483 more rows
library(reshape2)
## Warning: package 'reshape2' was built under R version 3.5.3
etf4.reorder = dcast(etf4.c, date~id)
## Using price as value column: use value.var to override.
dim(etf4.reorder)
## [1] 2474    5
head(etf4.reorder)
##       date  0050  0056 006205 00646
## 1 20090105 34.30 14.00     NA    NA
## 2 20090106 34.21 14.02     NA    NA
## 3 20090107 34.59 14.28     NA    NA
## 4 20090108 33.21 13.86     NA    NA
## 5 20090109 32.32 13.61     NA    NA
## 6 20090110 31.91 13.55     NA    NA
str(etf4.reorder)
## 'data.frame':    2474 obs. of  5 variables:
##  $ date  : num  20090105 20090106 20090107 20090108 20090109 ...
##  $ 0050  : num  34.3 34.2 34.6 33.2 32.3 ...
##  $ 0056  : num  14 14 14.3 13.9 13.6 ...
##  $ 006205: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 00646 : num  NA NA NA NA NA NA NA NA NA NA ...
etf4.reorder$date<-as.Date(as.character(etf4.reorder$date), "%Y%m%d") 
head(etf4.reorder)
##         date  0050  0056 006205 00646
## 1 2009-01-05 34.30 14.00     NA    NA
## 2 2009-01-06 34.21 14.02     NA    NA
## 3 2009-01-07 34.59 14.28     NA    NA
## 4 2009-01-08 33.21 13.86     NA    NA
## 5 2009-01-09 32.32 13.61     NA    NA
## 6 2009-01-10 31.91 13.55     NA    NA
str(etf4.reorder)
## 'data.frame':    2474 obs. of  5 variables:
##  $ date  : Date, format: "2009-01-05" "2009-01-06" ...
##  $ 0050  : num  34.3 34.2 34.6 33.2 32.3 ...
##  $ 0056  : num  14 14 14.3 13.9 13.6 ...
##  $ 006205: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ 00646 : num  NA NA NA NA NA NA NA NA NA NA ...
library(xts)
## Warning: package 'xts' was built under R version 3.5.3
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
etf4.xts<-xts(etf4.reorder[,-1], order.by = etf4.reorder$date)
head(etf4.xts)
##             0050  0056 006205 00646
## 2009-01-05 34.30 14.00     NA    NA
## 2009-01-06 34.21 14.02     NA    NA
## 2009-01-07 34.59 14.28     NA    NA
## 2009-01-08 33.21 13.86     NA    NA
## 2009-01-09 32.32 13.61     NA    NA
## 2009-01-10 31.91 13.55     NA    NA
tail(etf4.xts)
##             0050  0056 006205 00646
## 2018-12-22 74.75 24.15  25.08 22.93
## 2018-12-24 74.67 24.16  25.25 22.72
## 2018-12-25 73.57 23.90  24.90 22.51
## 2018-12-26 73.87 23.83  25.16 22.13
## 2018-12-27 74.81 23.96  25.30 22.95
## 2018-12-28 75.21 23.92  25.24 23.16
str(etf4.xts)
## An 'xts' object on 2009-01-05/2018-12-28 containing:
##   Data: num [1:2474, 1:4] 34.3 34.2 34.6 33.2 32.3 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:4] "0050" "0056" "006205" "00646"
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
##  NULL
etf4.xts<-na.locf(etf4.xts)                
tail(etf4.xts)
##             0050  0056 006205 00646
## 2018-12-22 74.75 24.15  25.08 22.93
## 2018-12-24 74.67 24.16  25.25 22.72
## 2018-12-25 73.57 23.90  24.90 22.51
## 2018-12-26 73.87 23.83  25.16 22.13
## 2018-12-27 74.81 23.96  25.30 22.95
## 2018-12-28 75.21 23.92  25.24 23.16
etf4.xts<-na.omit(etf4.xts)
head(etf4.xts)
##             0050  0056 006205 00646
## 2015-12-14 59.35 21.06  30.98 19.54
## 2015-12-15 59.59 21.25  31.66 19.70
## 2015-12-16 60.11 21.50  31.67 19.80
## 2015-12-17 60.78 21.76  32.06 20.05
## 2015-12-18 60.78 21.97  32.23 19.87
## 2015-12-21 60.31 21.99  32.62 19.64
library(quantmod)
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.5.3
## Version 0.4-0 included new data defaults. See ?getSymbols.
etf4_monthly <- to.monthly(etf4.xts, indexAt = "lastof", OHLC=FALSE)
head(etf4_monthly)
##             0050  0056 006205 00646
## 2015-12-31 60.75 21.81  31.97 20.07
## 2016-01-31 59.51 21.48  26.21 19.21
## 2016-02-29 61.25 22.39  25.90 19.22
## 2016-03-31 64.82 22.44  27.83 19.73
## 2016-04-30 61.63 21.58  27.17 19.82
## 2016-05-31 63.10 21.86  27.10 20.31
library(PerformanceAnalytics)
## Warning: package 'PerformanceAnalytics' was built under R version 3.5.3
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
library(magrittr)
etf4_returns_xts <-Return.calculate(etf4_monthly, method = "log") %>%
  na.omit()
head(etf4_returns_xts)
##                   0050        0056       006205         00646
## 2016-01-31 -0.02062272 -0.01524631 -0.198656946 -0.0437951860
## 2016-02-29  0.02881948  0.04149216 -0.011898049  0.0005204268
## 2016-03-31  0.05665035  0.00223065  0.071871607  0.0261889165
## 2016-04-30 -0.05046543 -0.03907812 -0.024001152  0.0045512089
## 2016-05-31  0.02357200  0.01289152 -0.002579696  0.0244218467
## 2016-06-30  0.03335201  0.03196311 -0.017119882 -0.0249265149
dim(etf4_returns_xts)
## [1] 36  4
plot(etf4_returns_xts, xaxt='n')
axis(1, index(etf4_returns_xts), format(index(etf4_returns_xts), "%Y/%m"))

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
etf4_ret.df1<-fortify(etf4_returns_xts)
head(etf4_ret.df1)
##        Index        0050        0056       006205         00646
## 1 2016-01-31 -0.02062272 -0.01524631 -0.198656946 -0.0437951860
## 2 2016-02-29  0.02881948  0.04149216 -0.011898049  0.0005204268
## 3 2016-03-31  0.05665035  0.00223065  0.071871607  0.0261889165
## 4 2016-04-30 -0.05046543 -0.03907812 -0.024001152  0.0045512089
## 5 2016-05-31  0.02357200  0.01289152 -0.002579696  0.0244218467
## 6 2016-06-30  0.03335201  0.03196311 -0.017119882 -0.0249265149
plot(etf4_ret.df1$`0050`, etf4_ret.df1$`00646`, pch=20,
     col = 'darkred', main = '0050 vs. 00646 monthly returns',
     xlab = '0050', ylab = '00646 S&P500')

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages ------------------------------------------------------------------ tidyverse 1.2.1 --
## √ tibble  2.0.1     √ purrr   0.3.1
## √ tidyr   0.8.3     √ stringr 1.4.0
## √ tibble  2.0.1     √ forcats 0.4.0
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts --------------------------------------------------------------------- tidyverse_conflicts() --
## x tidyr::extract()   masks magrittr::extract()
## x dplyr::filter()    masks stats::filter()
## x xts::first()       masks dplyr::first()
## x dplyr::lag()       masks stats::lag()
## x xts::last()        masks dplyr::last()
## x purrr::set_names() masks magrittr::set_names()
library(ggplot2)
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.5.3
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
etf4_ret.df2 <- cbind(etf4_ret.df1, month=month(index(etf4_returns_xts)), 
                      year=year(index(etf4_returns_xts)))
ggplot(data = etf4_ret.df2) +
  geom_point(mapping = aes(x = etf4_ret.df2$`0050`, y = etf4_ret.df2$`0056`, color = month))

ggplot(data = etf4_ret.df2) +
  geom_point(mapping = aes(x = etf4_ret.df2$`0050`, y = etf4_ret.df2$`0056`, size = month))

ggplot(data = etf4_ret.df2) +
  geom_point(mapping = aes(x = etf4_ret.df2$`0050`, y = etf4_ret.df2$`0056`, alpha = month))

etf4_ret<-coredata(etf4_monthly[-1,])/coredata(etf4_monthly[-dim(etf4_monthly)[1],])-1
head(etf4_ret)
##             0050        0056       006205         00646
## [1,] -0.02041152 -0.01513067 -0.180168908 -0.0428500249
## [2,]  0.02923878  0.04236499 -0.011827547  0.0005205622
## [3,]  0.05828571  0.00223314  0.074517375  0.0265348595
## [4,] -0.04921321 -0.03832442 -0.023715415  0.0045615813
## [5,]  0.02385202  0.01297498 -0.002576371  0.0247225025
## [6,]  0.03391442  0.03247941 -0.016974170 -0.0246184146
class(etf4_ret)
## [1] "matrix"
etf4_ret
##                0050         0056       006205         00646
##  [1,] -0.0204115226 -0.015130674 -0.180168908 -0.0428500249
##  [2,]  0.0292387834  0.042364991 -0.011827547  0.0005205622
##  [3,]  0.0582857143  0.002233140  0.074517375  0.0265348595
##  [4,] -0.0492132058 -0.038324421 -0.023715415  0.0045615813
##  [5,]  0.0238520201  0.012974977 -0.002576371  0.0247225025
##  [6,]  0.0339144216  0.032479414 -0.016974170 -0.0246184146
##  [7,]  0.0554874310  0.080194949  0.012012012  0.0312973246
##  [8,]  0.0124891083  0.013125513  0.024480712  0.0024473813
##  [9,]  0.0192197361  0.013765182 -0.038015930 -0.0219726562
## [10,]  0.0153391500 -0.053913738  0.020700038 -0.0054917624
## [11,] -0.0059598060 -0.018151119  0.057522124  0.0411646586
## [12,]  0.0006971556 -0.008598452 -0.058577406  0.0385728062
## [13,]  0.0197854257  0.019947962  0.018148148 -0.0129990715
## [14,] -0.0012296762  0.039115646  0.003273918  0.0145813735
## [15,]  0.0103967168  0.013093290 -0.031182016  0.0000000000
## [16,]  0.0083942594 -0.012116317 -0.015344311 -0.0023180343
## [17,]  0.0296723953  0.015944399  0.035347777  0.0041821561
## [18,]  0.0445951232  0.027766600  0.060939794  0.0143452106
## [19,]  0.0154787168  0.021534847  0.043944637  0.0159671533
## [20,]  0.0195451752  0.000000000  0.038448790 -0.0094297261
## [21,] -0.0227875573 -0.006132618 -0.006383658  0.0385312783
## [22,]  0.0502159161 -0.015426147  0.024734982  0.0139676997
## [23,] -0.0300751880 -0.020368194  0.010344828  0.0111924236
## [24,] -0.0064195736  0.000000000 -0.004964319  0.0166028097
## [25,]  0.0295014019  0.053178729  0.082008107  0.0335008375
## [26,] -0.0197750148 -0.012528474 -0.036599424 -0.0113452188
## [27,]  0.0013288234 -0.001922338 -0.056536045 -0.0385245902
## [28,] -0.0384847388 -0.030046225 -0.016487001  0.0166240409
## [29,]  0.0127979925  0.040508340 -0.001611863  0.0310272537
## [30,]  0.0012388503 -0.023664122 -0.083629319  0.0166734445
## [31,]  0.0570403366  0.057857701  0.009866103  0.0340000000
## [32,]  0.0155683015  0.022172949 -0.038381019  0.0398452611
## [33,]  0.0051867220 -0.013738250  0.030478955  0.0000000000
## [34,] -0.1145510836 -0.130865103 -0.076408451 -0.0662202381
## [35,] -0.0012950013  0.032053986  0.001143729  0.0139442231
## [36,] -0.0247665975 -0.022476502 -0.038842346 -0.0899803536
etf4_ret.tmp<-data.frame(date = index(etf4_returns_xts), etf4_ret)
head(etf4_ret.tmp)
##         date       X0050       X0056      X006205        X00646
## 1 2016-01-31 -0.02041152 -0.01513067 -0.180168908 -0.0428500249
## 2 2016-02-29  0.02923878  0.04236499 -0.011827547  0.0005205622
## 3 2016-03-31  0.05828571  0.00223314  0.074517375  0.0265348595
## 4 2016-04-30 -0.04921321 -0.03832442 -0.023715415  0.0045615813
## 5 2016-05-31  0.02385202  0.01297498 -0.002576371  0.0247225025
## 6 2016-06-30  0.03391442  0.03247941 -0.016974170 -0.0246184146
plot(etf4_ret.tmp$X0050, etf4_ret.tmp$X0056)

etf4_ret.df<-fortify(etf4_returns_xts, melt=TRUE)
head(etf4_ret.df)
##        Index Series       Value
## 1 2016-01-31   0050 -0.02062272
## 2 2016-02-29   0050  0.02881948
## 3 2016-03-31   0050  0.05665035
## 4 2016-04-30   0050 -0.05046543
## 5 2016-05-31   0050  0.02357200
## 6 2016-06-30   0050  0.03335201
p<-ggplot(etf4_ret.df, aes(x = Index, y = Value))+
  geom_line(aes(color = Series), size = 1)
p

p + scale_x_date(date_labels = "%Y/%m")

q<-etf4_ret.df %>%
  ggplot(aes(x =Value, fill = Series)) +
  geom_histogram(alpha = 0.45, binwidth = .005) +
  ggtitle("Monthly Returns")
q

q + facet_wrap(~Series)+ theme_update(plot.title = element_text(hjust = 0.5))

etf4_ret.df %>%
  ggplot(aes(x = Value, colour = Series)) +
  geom_density(alpha = 1) +
  ggtitle("Monthly Returns Density Since 2016") +
  xlab("monthly returns") +
  ylab("distribution") +
  theme_update(plot.title = element_text(hjust = 0.5))

etf4_ret.df %>%
  ggplot(aes(x = Value)) +
  geom_density(aes(color = Series), alpha = 1) +
  geom_histogram(aes(fill = Series), alpha = 0.45, binwidth = .01) +
  guides(fill = FALSE) +
  facet_wrap(~Series) +
  ggtitle("Monthly Returns Since 2016") +
  xlab("monthly returns") +
  ylab("distribution") +
  theme_update(plot.title = element_text(hjust = 0.5))

library(plotly)
## Warning: package 'plotly' was built under R version 3.5.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p1<-plot_ly(etf4_ret.tmp, X = ~date, y=~X0050,name = "0050",type='scatter',mode = 'lines')%>%
   add_trace(y=~X0056,name='0056',mode='lines+markers')%>%
   layout(xaxis=list(title='year'),yaxis=list(title='monthly returns'))
p1
## Warning: 'scatter' objects don't have these attributes: 'X'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
## Warning: 'scatter' objects don't have these attributes: 'X'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'mode', 'hoveron', 'line', 'connectgaps', 'cliponaxis', 'fill', 'fillcolor', 'marker', 'selected', 'unselected', 'textposition', 'textfont', 'r', 't', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'